var SoDao_UserCard_hideTimer = null;
var SoDao_UserCard_showTimer = null;
var SoDao_UserCard_lastObj = 'null';
(function ($) {
    $.fn.UserCard = function () {
        return this.each(function () {
            $.fn.UserCard.init($(this));
        });
    };
    //初始化绑定事件
    $.fn.UserCard.init = function ($this) {
        //图片层中带uid，才执行;
        var username = $this.attr("uid");
        if (!username) return;
        if (username.length == 0) return;

        var guid = $this.attr("guid");
        if (guid != undefined) return;

        //移动到头像图片层上
        $this.unbind("mouseenter").removeAttr('onmouseenter').mouseenter(function (event) {

            if (SoDao_UserCard_lastObj == guid) {
                clearTimeout(SoDao_UserCard_hideTimer);
                return;
            }

            //显示用户卡，等待500毫秒，避免快速操作不必要的开销
            SoDao_UserCard_showTimer = setTimeout(function () {
                //最后一次选择的头像图片层
                //SoDao_UserCard_lastObj = $this;
                if (typeof guid != 'undefined') {
                    SoDao_UserCard_lastObj = guid;
                }
                else {
                    guid = Math.random();
                    $this.attr("guid", guid);
                    SoDao_UserCard_lastObj = guid;
                }

                if ($("#u_card_loading").is(":hidden")) {
                    var Ctop = parseInt(($(window).height() + $(document).scrollTop() - $("#u-card").height()) > $this.offset().top ? $this.offset().top + ($this.height() / 2) : $this.offset().top - $("#u_card_loading").height() + ($this.height() / 2), 10);
                    var Cleft = parseInt(($(window).width() + $(document).scrollLeft() - $("#u-card").width()) > $this.offset().left ? $this.offset().left + ($this.width() / 2) : $this.offset().left - $("#u_card_loading").width() + ($this.width() / 2), 10);
                    $("#u_card_loading").show().css({ 'top': Ctop, 'left': Cleft });
                }
                $.fn.UserCard.loadData($this);
            }, 500);
            //$.fn.UserCard.loadData($this);
        }).unbind("mouseleave").removeAttr('onmouseleave').mouseleave(function () {
            //移出头像图片层,取消显示该用户卡
            clearTimeout(SoDao_UserCard_showTimer);
            //隐藏用户卡
            SoDao_UserCard_hideTimer = setTimeout($.fn.UserCard.HideCard, 10)
        });


        //移动到用户卡
        $("#u-card").unbind("mouseenter").removeAttr('onmouseenter').mouseenter(function () {
            //取消隐藏用户卡
            clearTimeout(SoDao_UserCard_hideTimer);
        }).unbind("mouseleave").removeAttr('onmouseleave').mouseleave(function () {
            //移出用户卡

            //取消显示用户卡
            clearTimeout(SoDao_UserCard_showTimer);
            //隐藏用户卡，等待500毫秒
            SoDao_UserCard_hideTimer = setTimeout($.fn.UserCard.HideCard, 300)
        });

        $("#u_card_loading").unbind("mouseenter").removeAttr('onmouseenter').mouseenter(function () {

            clearTimeout(SoDao_UserCard_hideTimer);
        }).unbind("mouseleave").removeAttr('onmouseleave').mouseleave(function () {
            clearTimeout(SoDao_UserCard_showTimer);
            SoDao_UserCard_hideTimer = setTimeout($.fn.UserCard.HideCard, 300)
        });
    }
    //显示用户卡
    $.fn.UserCard.showCard = function ($this) {
        //var Ctop = parseInt(($(window).height() + $(document).scrollTop() - $("#u-card").height()) > $this.offset().top + $this.height() ? $this.offset().top : $this.offset().top - $("#u-card").height() + $this.height(), 10);
        //var Cleft = parseInt(($(window).width() + $(document).scrollLeft() - $("#u-card").width()) > $this.offset().left + $this.width() ? $this.offset().left + $this.width() : $this.offset().left - $("#u-card").width() + $this.width(), 10)
        var Ctop = parseInt(($(window).height() + $(document).scrollTop() - $("#u-card").height()) > $this.offset().top ? $this.offset().top + ($this.height() / 2) : $this.offset().top - $("#u-card").height() + ($this.height() / 2), 10);
        var Cleft = parseInt(($(window).width() + $(document).scrollLeft() - $("#u-card").width()) > $this.offset().left ? $this.offset().left + ($this.height() / 2) : $this.offset().left - $("#u-card").width() + ($this.width() / 2), 10);
        $("#u-card").show().css({ 'top': Ctop, 'left': Cleft });
        //$("#u-card").show().css({ 'top': event.clientY + $(document).scrollTop(), 'left': event.clientX + $(document).scrollLeft() });
    };
    //加载数据
    $.fn.UserCard.loadData = function ($this) {
        var username = $($this).attr("uid");
        var guid = $this.attr("guid");
        $.ajax({
            type: "GET",
            url: "/user/UCard_Data?u=" + username,
            dataType: "html",
            success: function (udate) {
                //登录用户=当前用户名，或者用户卡用户有异常，取消显示用户卡
                if (udate.indexOf("error") >= 0) {
                    $.fn.UserCard.HideCard();
                    $("#u_card_loading").hide();
                }
                else {
                    //当前加载完数据用户卡和最后选择对象相同才显示用户卡。避免加载中选则了其他用户卡，造成一次显示2张卡的效果。
                    if (SoDao_UserCard_lastObj == guid) {
                        $("#u-card").html(udate);
                        $.fn.UserCard.showCard($this);
                        //重新绑定在线状态图标
                        batchGetUserStatus([username], function (jsonData) {
                            $.each(jsonData, function (i) {
                                $("#u-card img[uid='" + jsonData[i].u + "']").show();
                            });
                        });
                        $("#u_card_loading").hide();
                    }
                    //else
                    //{

                    //}
                }
            }
        });
    };

    $.fn.UserCard.HideCard = function ($this) {
        $("#u-card").hide();
        //隐藏等待
        $("#u_card_loading").hide();
        SoDao_UserCard_lastObj = 'null';
    };

})(jQuery);

//轮询绑定事件，保证一些异步JS生成的头像加载不了的情况
function SodaoUserCardAutoBindEvent() {
    $(".user-avatar,.user-avatar-small").UserCard();
    setTimeout(SodaoUserCardAutoBindEvent, 2000)
}

//生成用户卡与用户卡等待区域层，不存在才创建，开始轮询绑定事件
$(document).ready(function () {
    if ($("#u-card").length == 0) {
        var u_card = $("<div id=\"u-card\"></div>");
        $("body").append(u_card);
    }
    if ($("#u_card_loading").length == 0) {
        var u_card_loading = "<div id=\"u_card_loading\">用户信息加载中...</div>";
        $("body").append(u_card_loading);
    }
    SodaoUserCardAutoBindEvent();
});

//无用，网上找到鼠标定位的代码
function mousePosition(ev) {
    if (ev.pageX || ev.pageY) {
        return { x: ev.pageX, y: ev.pageY };
    }
    return {
        x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y: ev.clientY + document.body.scrollTop - document.body.clientTop
    };
}

function mouseMove(obj, ev) {
    ev = ev || window.event;
    var mousePos = mousePosition(ev);
    obj.css({ 'top': event.screenX, 'left': event.screenY });
}